package com.k1ccc0.alumniinformationmanagement.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.k1ccc0.alumniinformationmanagement.mapper.AlumniMapper;
import com.k1ccc0.alumniinformationmanagement.pojo.Alumni;
import com.k1ccc0.alumniinformationmanagement.pojo.PageBean;
import com.k1ccc0.alumniinformationmanagement.service.AlumniService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class AlumniServiceImpl implements AlumniService {

    @Autowired
    private AlumniMapper alumniMapper;

    @Override
    public PageBean selectByPage(Integer page, Integer pageSize, String name, String username, Short gender,
                                 String major, String department, String nativePlace, String phoneNumber, String email) {
        //1、设置分页参数
        PageHelper.startPage(page,pageSize,true);

        //2、执行查询
        List<Alumni> alumniList = alumniMapper.list(name, username, gender, major, department,nativePlace, phoneNumber, email);
        Page<Alumni> p = (Page<Alumni>) alumniList;
        //3、将两个结果封装到PageBean对象中
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    @Override
    public Alumni userLogin(Alumni alumni) {
        return alumniMapper.getUsernameAndPassword(alumni);
    }

    @Override
    public void update(Alumni alumni) {
        alumni.setUpdateTime(LocalDateTime.now());
        alumniMapper.update(alumni);
    }


    @Override
    public Alumni selectUserInfo(String username) {

        return alumniMapper.getByUsername(username);
    }

    @Override
    public Alumni selectUserInfoByUAndP(String username) {

        return alumniMapper.selectUserInfoByUAndP(username);
    }

    @Override
    public void updateAvatar(Alumni alumni) {
        alumniMapper.updateAvatar(alumni);
    }

    @Override
    public void updateUsernameById(Alumni alumni) {
        alumniMapper.updateUsernameById(alumni);
    }

    @Override
    public void updatePasswordById(Alumni alumni) {
        alumniMapper.updatePasswordById(alumni);
    }

    @Override
    public void add(Alumni alumni) {
        alumni.setUpdateTime(LocalDateTime.now());
        alumni.setCreateTime(LocalDateTime.now());
        alumniMapper.add(alumni);
    }

    @Override
    public void delete(List<Integer> ids) {
        alumniMapper.deleteById(ids);
    }
    
    @Override
    public List<Alumni> getAllAlumniLocations() {
        return alumniMapper.getAllLocations();
    }
}
